package org.mca.mcaweb.bean;

import i18n.MsgUtil;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;

import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mca.mcaweb.dao.DAO;
import org.mca.mcaweb.dao.UsuaDAO;
import org.mca.mcaweb.entity.Empr;
import org.mca.mcaweb.entity.Usua;
import org.mca.mcaweb.util.PasswordHelper;
import org.mca.mcaweb.util.ResourceBundleUtil;


@Named
@RequestScoped
public class LoginBean implements Serializable {

	private static final long serialVersionUID = -4805577368907174509L;

	private ResourceBundle bundle = ResourceBundleUtil.getResourceBunde();
	
	private Usua usua = new Usua();
	private Empr empr = new Empr();
	
	private List<Empr> emprs;
	
	@Inject
	private LoggedUser loggedUser;

	@Inject
	private UsuaDAO dao;
	
	@Inject
	private DAO<Empr> daoEmpr;
	
	public String login() {
		
		this.usua.setUsuasenha(PasswordHelper.getInstance().encryptMD5(this.usua.getUsuasenha()));
		
		Usua bdUser = dao.verifyUsua(this.usua);

		if (bdUser != null) {
			if (bdUser.getUsuaativo().equals("S")) {
				loggedUser.setUsua(bdUser);
				loggedUser.setEmpr(empr);
				
				// Adicionar o usuário na sessão para que possa aparecer no manager do tomcat
				FacesContext fc = FacesContext.getCurrentInstance();
				HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);// request.getSession();
				HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();

				session.setAttribute("username", loggedUser.getUsua().getUsuanome() + " - " + request.getRemoteAddr());
				session.setAttribute("locale", fc.getExternalContext().getRequestLocale());
				
				return "/pages/index?faces-redirect=true";
			} else {
				loggedUser.setUsua(null);
				MsgUtil.addErrorMessage(bundle.getString("login.error.inactive"), bundle.getString("login.error.inactive.detail"));
				return "#";
			}
		} else {
			loggedUser.setUsua(null);
			MsgUtil.addErrorMessage(bundle.getString("label.error.login"),bundle.getString("label.error.login.detail"));
			return "#";
		}
	}
	
	public String loginEmpr() {
		if (loggedUser.getUsua() != null && empr != null) {
			if (loggedUser.getUsua().getUsuacodigo() != null && empr.getEmprcodigo() != null) {
				if (!empr.getEmprcodigo().equals("")){
					loggedUser.setEmpr(empr);
					return "/pages/index?faces-redirect=true";	
				}
			} else {
				loggedUser.setEmpr(null);
				MsgUtil.addErrorMessage(bundle.getString("label.error.login"), bundle.getString("label.error.login.detail"));
				return "#";
			}
		}
		return "#";
	}
	
	public String logout() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);// request.getSession();
		session.invalidate();
		loggedUser.setUsua(null);
		return "/pages/login?faces-redirect=true";
	}
	
	public List<Empr> getcomboEmpr(){
		if (this.emprs == null){
			this.emprs = daoEmpr.listAll();
		}
		return this.emprs;
	}
	
	public boolean isLoggedUser() {
		return loggedUser.isLogged();
	}

	public Usua getUsua() {
		return usua;
	}

	public void setUsua(Usua usua) {
		this.usua = usua;
	}

	public Empr getEmpr() {
		return empr;
	}

	public void setEmpr(Empr empr) {
		this.empr = empr;
	}

	public List<Empr> getEmprs() {
		return emprs;
	}

	public void setEmprs(List<Empr> emprs) {
		this.emprs = emprs;
	}
}
